<!DOCTYPE html>
<html>
<head>
	<!-- Google Tag Manager -->
	<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
	j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
	'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
	})(window,document,'script','dataLayer','GTM-W8KNPT4');</script>
	<!-- End Google Tag Manager -->
	<meta charset="utf-8">
    <meta name="viewport" content="width=device-width,inital-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,minimal-ui">
    <meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
	<meta name="format-detection" content="telephone=no">
    <title>博猫体育</title>
    <link rel="stylesheet" href="css/app.min.css?ver=1.0.000">
    <link rel="stylesheet" href="css/iconfont.css?ver=1.0.000">
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
    <script src="js/pageLib.js"></script>
</head>
<body>
<div class="header">
	<div class="nav">
		<div class="title">提现</div>
		<a href="me.html" class="left-btn"><i class="iconfont icon-xitongfanhui icon-middle"></i></a>
		<div class="right-btn login-bar">登录 / 注册</div>
		<div class="right-btn user-info-bar" style="display:none;">
			<div class="login flexer">
				<div class="balance">
					<div>余额</div>
					<div>￥888.88</div>
				</div>
				<div class="deposit"><a href="deposit.html">充值</a></div>
			</div>
		</div>
	</div>
</div>
<div class="table-content" style="padding-top:45px;padding-bottom:120px;">
	
	<form class="withdrawal-form" id="withdrawal-form">
		<ul class="form-list">
			<li class="form-row">
				<label>钱包地址</label>
				<input type="text" name="address" value="" placeholder="只支持ETH钱包地址">
			</li>
			<li class="form-row">
				<label>提现金额</label>
				<input type="text" name="amount" value="" placeholder="最低提现￥100.00">
				<a href="javascript:;" class="function">提现全部</a>
			</li>
			<li class="form-row">
				<label>交易费</label>
				<input type="text" name="fee" data-fee="0.00021" value="0.00021 ETH" readonly>
			</li>
			<li class="form-row">
				<label>到账金额</label>
				<input type="text" name="net-amount" value="" placeholder="= 提现金额 - 交易费" readonly>
			</li>
		</ul>
		<ul class="form-list" id="verification-form">
			<li class="form-row">
				<label>手机号</label>
				<input type="text" name="mobile" value="加载中..." data-mobile="" readonly>
				<a href="#" class="function">获取短信验证码</a>
			</li>
			<li class="form-row">
				<label>验证码</label>
				<input type="text" name="verify-code" placeholder="请输入短信验证码">
			</li>
		</ul>
		<a href="#" class="submit">提交</a>
	</form>
</div>

</body>
<script type="text/javascript">
$(document).ready(function(){
	var calcNetAmount=function(){
		var rmbamount=parseFloat($('#withdrawal-form input[name=amount]').val());
		var price=parseFloat($('#withdrawal-form input[name=net-amount]').data('price'));
		var fee=parseFloat($('#withdrawal-form input[name=fee]').data('fee'));
		var etheramount=rmbamount/price;
		var netAmount=etheramount-fee;
		$('#withdrawal-form input[name=net-amount]').val('约 '+netAmount.toFixed(5)+' ETH');
	};
	pageLib.loadUserInfo();
	pageLib.api({
		action: '/v1/user/get-user-info?t='+(new Date()).valueOf(),
		success: function(resp){
			var withdrawableBalance='￥'+(resp.data.balance);
			$('#withdrawal-form input[name=amount]').attr('placeholder','可提金额 '+withdrawableBalance).next('.function').on('click',function(){
				$('#withdrawal-form input[name=amount]').val(resp.data.balance);
				calcNetAmount();
			});

			var desenstizationMobile=resp.data.mobile.substr(0,4)+'****'+resp.data.mobile.substr(-4);
			$('#verification-form input[name=mobile]').val(desenstizationMobile)
			.next('.function').on('click',function(){
				var target=$(this);
				if(target.hasClass('disable')){
					return;
				}
				else{
					target.addClass('disable');
				}
				pageLib.api({
					action:'/v1/real-name/get-real-sms',
					data:{
						"sms_type": 3
					},
					success: function(resp){
						target.html('<span>60</span>秒后可重发');
						var counter=setInterval(function(){
								target.find('span').text(target.find('span').text()-1);
						},1000);
						setTimeout(function(){
							clearInterval(counter);
							if(target.hasClass('disable'))target.removeClass('disable');
							target.text('重发验证码');
						},60000);
					},
					failed: function(){
						// alert('验证短信发送失败：请不要频繁发送验证短信。');
						target.html('请<span>60</span>秒后再试');
						var counter=setInterval(function(){
								target.find('span').text(target.find('span').text()-1);
						},1000);
						setTimeout(function(){
							clearInterval(counter);
							if(target.hasClass('disable'))target.removeClass('disable');
							target.text('获取验证码');
						},60000);
					}
				});
			});
			$('#withdrawal-form a.submit').on('click',function(){
				var submitBtn=$(this);
				if(submitBtn.hasClass('disable'))return;
				else submitBtn.addClass('disable').text('提交中...');
				var cash=parseFloat($('#withdrawal-form input[name=amount]').val());
				var verifyCode=$('#withdrawal-form input[name=verify-code]').val();
				var address=$('#withdrawal-form input[name=address]').val()
				pageLib.api({
					action: '/v1/real-name/set-withdraw',
					data: {
						cash: cash,
						code: verifyCode,
						type: 4,
						memo: address
					},
					success: function(resp){
						console.log(resp);
						window.location.href="withdrawal-result.html?status=success&amount="+cash+"&finishtime="+resp.data.time;
					},
					failed: function(){
						submitBtn.removeClass('disable').text('提交');
					},
					error: function(XMLHttpRequest,err,exeption){
						if(XMLHttpRequest.status==401){
							pageLib.alert('未登录，请先登录');
							submitBtn.removeClass('disable').text('提交');
						}
					}
				});
			});
		},
		error: function(XMLHttpRequest,err,exeption){
			if(XMLHttpRequest.status==401){
				window.location.href="login.html";
			}
		}
	});
	$.ajax({
			url: '/api/index.php/Api/getPrice',
			type: 'post',
			dataType:'json',
			success: function(resp){
				var price=resp.data.price;
				$('#withdrawal-form input[name=net-amount]').attr('data-price',price);
				$('#withdrawal-form input[name=amount]').on('keyup',calcNetAmount);
			}
		});
});
</script>
</html>